package com.tinyseed.emcp.api.productionRule.bean;

import com.tinyseed.emcp.api.basicdata.domain.convert.DTOConvert;
import com.tinyseed.emcp.api.productionRule.entity.Reduction;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

/**
 * Enterprise
 *
 * @author tinyCode
 * @date 2019-11-20 17:13:12
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReductionInputDTO implements Serializable {

    /**
     * 主键
     */
    @Id
    @GeneratedValue
    private Long id;

    /**
     * 设置类型，0：限产，1：停产
     */
    private Long produceType;

    /**
     * 减产量（%）
     */
    private Double lessProduce;

    /**
     * 开始时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startTime;

    private String startCreateTime;

    /**
     * 结束时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;

    private String endCreateTime;

    /**
     * 备注
     */
    private String remark;

    /**
     * 是否启用，0：禁用，1：启用
     */
    private Long active;

    /**
     * 预警等级：1黄色预警，2橙色预警，3红色预警
     */
    private Long level;

    /**
     * 是否删除：0：正常，1：删除
     */
    private String isDeleted;

    /**
     * 创建时间
     */
    private Timestamp createTime;

    /**
     * 最后修改时间
     */
    private Timestamp modifyTime;

    /**
     * 创建人
     */
    private String creator;

    /**
     * 最后操作人
     */
    private String modifier;
    public Reduction convertToReduction() {
        ReductionInputDTOConvert convert = new ReductionInputDTOConvert();
        Reduction result = convert.convert(this);
        return result;
    }

    private static class ReductionInputDTOConvert implements DTOConvert<ReductionInputDTO, Reduction> {

        @Override
        public Reduction convert(ReductionInputDTO reductionInputDTO) {
            Reduction reduction = new Reduction();
            BeanUtils.copyProperties(reductionInputDTO, reduction);
            reduction.setStartTime(reductionInputDTO.getStartTime());
            reduction.setEndTime(reductionInputDTO.getEndTime());
            Timestamp nowTime = new Timestamp(new Date().getTime());
            if (reduction.getId() == null || reduction.getCreateTime() == null) {
                reduction.setCreateTime(nowTime);
            }
            reduction.setModifyTime(nowTime);
            return reduction;
        }
    }

}
